package com.aishua.fire.activiti;


import java.util.List;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ActivitiUtil {

	@Autowired
	private TaskService taskService;
	@Autowired    
	private RuntimeService runtimeService;
	
	 /**
     * 根据taskId查找businessKey
     */
    public String getBusinessKeyByTaskId(String taskId){
        Task task = taskService
                .createTaskQuery()
                .taskId(taskId)
                .singleResult();
        ProcessInstance pi = runtimeService
                .createProcessInstanceQuery()
                .processInstanceId(task.getProcessInstanceId())
                .singleResult();
        return pi.getBusinessKey();
    }
    
    
    public String getTaskIdByProcId(String procId) {
    	Task task = taskService.createTaskQuery().processInstanceId(procId).singleResult();
		log.info("当前任务id:{}",task.getId());
		return task.getId();
    }
    
    //查询当前人的所有任务
    public void queryTaskByAssignee(String userId) {
    	String assignee=null;
    	List<Task> taskList=taskService.createTaskQuery()
    				.processDefinitionId(userId)//根据流程定义id查询
    				.taskAssignee(assignee)//根据办理人查询
    				.orderByTaskCreateTime()
    				.desc()
    				.list();
    	for(Task task :taskList) {
    		log.info("任务id:{}",task.getId());
    		log.info("任务名称:{}",task.getName());
    		log.info("办理人:{}",task.getAssignee());
    	}
    	
    }
}
